﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using NLBS.Data.Business;
using NLBS.Entity;
using System.Xml;
using NLBS.Web;
namespace NLBS.Blog.Plugins.TestPlugin
{
    public partial class TestPlugin : System.Web.UI.UserControl, IPlugin
    {


        #region IPlugin Members

        void IPlugin.Install()
        {
            Widget widget = new Widget();
            widget.Hidden = false;
            widget.Type = 1;
            widget.Title = "TestPlugin";
            widget.Name = "Test Plugin";
            widget.Order = 1;
            widget.InstallTime = DateTime.Now;
            widget.Content = string.Empty;
            WidgetData.InsertWidget(widget);
            XmlDocument doc = SiteConfig.GetExtensionsXml();
            XmlNode root = doc.DocumentElement;

            XmlElement elem = doc.CreateElement("PluginID");
            elem.InnerText = widget.WidgetID.ToString();
            
            XmlNode plugin = root.SelectSingleNode(string.Format("descendant::Plugin[Name='{0}']", widget.Title));
            plugin.AppendChild(elem);
            XmlNode name = plugin.SelectSingleNode("descendant::Enabled");
            name.InnerText = "False";
            
            SiteConfig.SaveExtensionsXml(doc);
        }

        void IPlugin.UnInstall()
        {
            XmlDocument doc = SiteConfig.GetExtensionsXml();
            XmlNode root = doc.DocumentElement;
            XmlNode plugin = root.SelectSingleNode("descendant::Plugin[Name='TestPlugin']");
            XmlNode PluginIDNode=plugin.SelectSingleNode("descendant::PluginID");
            int widgetID = Convert.ToInt32(PluginIDNode.InnerText);
            WidgetData.DeleteWidget(widgetID);
            plugin.SelectSingleNode("descendant::Enabled").InnerText = "True";
            plugin.RemoveChild(PluginIDNode);
            SiteConfig.SaveExtensionsXml(doc);
        }
        public void Update()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}